﻿using RightControl.Common;
using RightControl.IRepository;
using RightControl.IService;
using RightControl.Model;
using System.Collections.Generic;

namespace RightControl.Service {
    public class ProductImgService : BaseService<ProductImage>, IProductImgService {
        public IProductImgRepository repository { get; set; }

        public int SaveImages(IEnumerable<ProductImage> entitys) {
            return repository.SaveImages(entitys);
        }
        public dynamic GetListByFilter(ProductImage filter, PageInfo pageInfo) {
            string _where = " where 1=1";
            _where += string.Format(" and ProductId = {0}", filter.ProductId);
            if (filter.ImgType > 0) {
                _where += string.Format(" and ImgType = {0}", filter.ImgType);
            }

            pageInfo.field = "Sort";
            pageInfo.order = "Asc";
            
            return GetListByFilter(filter, pageInfo, _where);
        }

        public bool DeleteByProdcutId(int productId, int userId = 0, int imgType = 0) {
            string _where = " where 1=1";
            _where += string.Format(" and ProductId = {0}", productId);

            if (imgType > 0) {
                _where += string.Format(" and ImgType = {0}", imgType);
            }

            if (userId > 0) {
                _where += string.Format(" and CreateBy = {0}", userId);
            }
            return DeleteByWhere(_where);
        }

        public int UpdateProductImgSort(IEnumerable<ProductImage> entitys) {
            return repository.UpdateProductImgSort(entitys);
        }
    }
}
